package com.smartEdu.RongYun;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.model.UserStudentParamModel;
import com.smartEdu.utils.JsonUtil;


public class RongYun {  

    public static String GetRongCloudToken(String username) { 
    	Map jsonMap=null;
        StringBuffer res = new StringBuffer();  
        String token="";
        String url = "https://api.cn.ronghub.com/user/getToken.json";  
        String App_Key = "lmxuhwagxllwd"; 
        String App_Secret = "mXZ8ilDkglG";  
        String Timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳，从 1970 年 1 月 1 日 0 点 0 分 0 秒开始到现在的秒数。  
        String Nonce = String.valueOf(Math.floor(Math.random() * 1000000));//随机数，无长度限制。  
        String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。  
        HttpClient httpClient = new DefaultHttpClient();  
        HttpPost httpPost = new HttpPost(url);  
        httpPost.setHeader("App-Key", App_Key);  
        httpPost.setHeader("Timestamp", Timestamp);  
        httpPost.setHeader("Nonce", Nonce);  
        httpPost.setHeader("Signature", Signature);  
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");  
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);  
        nameValuePair.add(new BasicNameValuePair("userId",username)); 
        nameValuePair.add(new BasicNameValuePair("name","weishengxing"));
        HttpResponse httpResponse = null;  
       try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"utf-8"));  
            httpResponse = httpClient.execute(httpPost);  
            HttpEntity entity = httpResponse.getEntity(); 
            String jsonStr = EntityUtils.toString(entity);   
            System.out.println(jsonStr);
            jsonMap=JsonUtil.toMap(jsonStr);
            
//            JSONObject obj=JSONObject.fromObject(jsonStr);
//            token=obj.getString(jsonMap.get("token").toString()); 
            token=jsonMap.get("token").toString();
            
       } catch (IOException e) {  
          // e.printStackTrace();  
        }  
        return token;
    } 
    public  Boolean CreateOrAddGroup(String id,String groupid,String name,String url){
    	Map jsonMap=null;
        StringBuffer res = new StringBuffer();  
        Boolean flag=false;
       // String url = "http://api.cn.ronghub.com/group/create.json";  
        String App_Key = "lmxuhwagxllwd"; 
        String App_Secret = "mXZ8ilDkglG";  
        String Timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳，从 1970 年 1 月 1 日 0 点 0 分 0 秒开始到现在的秒数。  
        String Nonce = String.valueOf(Math.floor(Math.random() * 1000000));//随机数，无长度限制。  
        String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。  
        HttpClient httpClient = new DefaultHttpClient();  
        HttpPost httpPost = new HttpPost(url);  
        httpPost.setHeader("App-Key", App_Key);  
        httpPost.setHeader("Timestamp", Timestamp);  
        httpPost.setHeader("Nonce", Nonce);  
        httpPost.setHeader("Signature", Signature);  
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");  
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);  
        nameValuePair.add(new BasicNameValuePair("userId",id)); 
        nameValuePair.add(new BasicNameValuePair("groupId",groupid));
        nameValuePair.add(new BasicNameValuePair("groupName",name));
        HttpResponse httpResponse = null;  
       try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"utf-8"));  
            httpResponse = httpClient.execute(httpPost);  
            HttpEntity entity = httpResponse.getEntity(); 
            String jsonStr = EntityUtils.toString(entity);   
            System.out.println(jsonStr);
            jsonMap=JsonUtil.toMap(jsonStr);
            if(jsonMap.get("code").equals(200)){
            	flag=true;
            }else{
            	flag=false;
            }
            
//            JSONObject obj=JSONObject.fromObject(jsonStr);
//            token=obj.getString(jsonMap.get("token").toString()); 
            
       } catch (IOException e) {  
           e.printStackTrace();  
        }  
       return flag;
    	
    }
    
    public  Boolean QueryGroup(String groupid){
    	Map jsonMap=null;
        StringBuffer res = new StringBuffer();  
        Boolean flag=false;
        String url = "http://api.cn.ronghub.com/group/user/query.json";  
        String App_Key = "lmxuhwagxllwd"; 
        String App_Secret = "mXZ8ilDkglG";  
        String Timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳，从 1970 年 1 月 1 日 0 点 0 分 0 秒开始到现在的秒数。  
        String Nonce = String.valueOf(Math.floor(Math.random() * 1000000));//随机数，无长度限制。  
        String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。  
        HttpClient httpClient = new DefaultHttpClient();  
        HttpPost httpPost = new HttpPost(url);  
        httpPost.setHeader("App-Key", App_Key);  
        httpPost.setHeader("Timestamp", Timestamp);  
        httpPost.setHeader("Nonce", Nonce);  
        httpPost.setHeader("Signature", Signature);  
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");  
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);   
        nameValuePair.add(new BasicNameValuePair("groupId",groupid));
   
        HttpResponse httpResponse = null;  
       try {  
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"utf-8"));  
            httpResponse = httpClient.execute(httpPost);  
            HttpEntity entity = httpResponse.getEntity(); 
            String jsonStr = EntityUtils.toString(entity);   
            System.out.println(jsonStr);
            jsonMap=JsonUtil.toMap(jsonStr);
            
            System.out.println(jsonMap.get("users"));
            if(jsonMap.get("code").equals(200)){
            	flag=true;
            }else{
            	flag=false;
            }
            
//            JSONObject obj=JSONObject.fromObject(jsonStr);
//            token=obj.getString(jsonMap.get("token").toString()); 
            
       } catch (IOException e) {  
           e.printStackTrace();  
        }  
       return flag;
    	
    }
    //SHA1加密//http://www.rongcloud.cn/docs/server.html#通用_API_接口签名规则  
    private static String sha1(String data){  
        StringBuffer buf = new StringBuffer();  
        try{  
            MessageDigest md = MessageDigest.getInstance("SHA1");  
            md.update(data.getBytes());  
            byte[] bits = md.digest();  
            for(int i = 0 ; i < bits.length;i++){  
                int a = bits[i];  
                if(a<0) a+=256;  
                if(a<16) buf.append("0");  
                buf.append(Integer.toHexString(a));  
            }  
        }catch(Exception e){  
  
        }  
        return buf.toString();  
    }  
}  
